using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Services
{
    /// <summary>
    /// 用户服务实现
    /// 提供用户管理相关的业务逻辑实现，包括注册、登录和用户信息查询等功能
    /// </summary>
    public class UserService : IUserAppService
    {
        /// <summary>
        /// 用户仓储接口
        /// </summary>
        private readonly Domain.Interfaces.IUserRepository _userRepository;
        
        /// <summary>
        /// 初始化用户服务实例
        /// </summary>
        /// <param name="userRepository">用户仓储接口实例</param>
        public UserService(Domain.Interfaces.IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        /// <summary>
        /// 用户注册
        /// 处理新用户的注册逻辑，包括用户名验证和密码加密
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="email">邮箱地址</param>
        /// <param name="password">明文密码</param>
        /// <param name="phone">手机号码，可选</param>
        /// <returns>注册操作的异步任务</returns>
        public async Task RegisterAsync(string username, string email, string password, string? phone = null)
        {
            // 注册逻辑，调用仓储
            await _userRepository.RegisterAsync(username, email, password, phone);
        }

        /// <summary>
        /// 用户登录
        /// 验证用户凭据并生成访问令牌
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">明文密码</param>
        /// <returns>成功时返回访问令牌</returns>
        public async Task<string> LoginAsync(string username, string password)
        {
            // 登录逻辑，调用仓储
            return await _userRepository.LoginAsync(username, password);
        }

        /// <summary>
        /// 获取当前用户信息
        /// 根据当前认证上下文获取用户详细信息
        /// </summary>
        /// <returns>当前用户DTO，如果不存在则返回null</returns>
        public async Task<Application.DTOs.UserDto?> GetCurrentUserAsync()
        {
            // 获取当前用户逻辑，调用仓储
            var user = await _userRepository.GetCurrentUserAsync();
            // 可加 DTO 映射
            return user;
        }
    }
}
